import line from "../../../../../../canvas/common/struct/basestruct/polyline";
import arc from "../../../../../../canvas/common/struct/basestruct/arc";
import polygon from "../../../../../../canvas/common/struct/basestruct/polygon";
import pathinfo from "../../../../../../canvas/common/struct/basestruct/pathinfo";
import coord from "../../../../../../canvas/common/struct/coord";

import globalconst from "../../../../../../canvas/common/globalConst";
var global = globalconst.GlobalConst;
import utilfuncs from "../../../../../../canvas/common/tools/utilfuncs";
var funcs = utilfuncs.UtilFuncs;
import { SvgPath2LGXPathSvc } from "./svgPath2LGXPathSvc";

var SvgPath2InfoSvc = {}

SvgPath2InfoSvc.chg2LGXPathGraph = function (secUnitList, refParser) {
    let infoLst = []
    secUnitList.forEach(unit => {
        this.chgUnit2GraphInfo(unit, infoLst, refParser);
    });
    let pathDesc = SvgPath2LGXPathSvc.convert(infoLst);
    return pathDesc;
}

SvgPath2InfoSvc.chgUnit2GraphInfo = function (unit, infoLst, refParser) {
    switch (unit.type) {
        case 'line':
            this.chg2LineInfo(unit, infoLst, refParser);
            break;

        case 'arc':
            this.chg2ArcInfo(unit, infoLst, refParser);
            break;

        case 'bezier':
            this.chg2BezierInfo(unit, infoLst, refParser);
            break;

        default:
            break;
    }
}

SvgPath2InfoSvc.chg2ArcInfo = function (unit, infoLst, refParser) {
    let unitInfo = new arc.Arc();
    unitInfo.type = global.BaseUnitArc << 16;

    let p1 = { x: unit.st.x, y: unit.st.y };
    let p2 = { x: unit.param.endx, y: unit.param.endy };

    refParser.chg2SysCoord(p1);
    refParser.chg2SysCoord(p2);

    let radius = unit.param.rx;
    let circlepos1 = new coord.CirclePos();
    circlepos1.x = p1.x;
    circlepos1.y = p1.y;
    circlepos1.radius = radius;

    let circlepos2 = new coord.CirclePos();
    circlepos2.x = p2.x;
    circlepos2.y = p2.y;
    circlepos2.radius = radius;

    let rstArr = funcs.getCircleInterCircle(circlepos1, circlepos2);
    if (!rstArr || (rstArr && rstArr.length != 2)) {
        unitInfo = null;
        return;
    }

    let interPt1 = rstArr[0].pt;
    let interPt2 = rstArr[1].pt;

    let arcPos = new coord.ArcPos();
    arcPos.x = interPt1.x;
    arcPos.y = interPt1.y;
    arcPos.radius = radius;

    let vector1 = funcs.getVector(interPt1, p1);
    arcPos.stAngle = vector1.angleFrmXAxisPositiveDir();

    let vector2 = funcs.getVector(interPt1, p2);
    arcPos.endAngle = vector2.angleFrmXAxisPositiveDir();

    unitInfo.radius = arcPos.radius;
    unitInfo.x = arcPos.x;
    unitInfo.y = arcPos.y;
    unitInfo.stAngle = arcPos.stAngle;
    unitInfo.endAngle = arcPos.endAngle;
    unitInfo.antiClockWise = arcPos.antiClockWise;
    unitInfo.setPos(arcPos);
    arcPos = null;

    infoLst.push(unitInfo);
}

SvgPath2InfoSvc.chg2LineInfo = function (unit, infoLst, refParser) {
    let unitInfo = new line.Polyline();
    unitInfo.type = global.BaseUnitPolyline << 16;
    let p1 = { x: unit.st.x, y: unit.st.y };
    let p2 = { x: unit.end.x, y: unit.end.y };
    refParser.chg2SysCoord(p1);
    refParser.chg2SysCoord(p2);
    unitInfo.pos.addPoint(p1);
    unitInfo.pos.addPoint(p2);
    unitInfo.strPosition = unitInfo.pos.toString();
    infoLst.push(unitInfo);
}

SvgPath2InfoSvc.chg2BezierInfo = function (unit, infoLst, refParser) {
    let unitInfo = new polygon.Polygon();
    unitInfo.type = global.BezierCurve << 16;
    let p1 = { x: unit.st.x, y: unit.st.y };
    let p2 = { x: unit.param.x1, y: unit.param.y1 };
    let p3 = { x: unit.param.x2, y: unit.param.y2 };
    let p4 = { x: unit.param.x, y: unit.param.y };
    refParser.chg2SysCoord(p1);
    refParser.chg2SysCoord(p2);
    refParser.chg2SysCoord(p3);
    refParser.chg2SysCoord(p4);
    unitInfo.pos.addPoint(p1);
    unitInfo.pos.addPoint(p2);
    unitInfo.pos.addPoint(p3);
    unitInfo.pos.addPoint(p4);
    unitInfo.strPosition = unitInfo.pos.toString();
    infoLst.push(unitInfo);
}

export { SvgPath2InfoSvc }